package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class jdbcTest1 {
	// 数据库地址
	private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/students?useSSL=false&serverTimezone=CTT";
	// 用户名
	private static String dbUserName = "ikun";
	// 密码
	private static String dbPassword = "ikun2017";
	// 驱动名称
	private static String jdbcName = "com.mysql.cj.jdbc.Driver";
 
	public static void main(String[] args) {
		try {
			Class.forName(jdbcName);
			System.out.println("加载驱动成功！");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("加载驱动失败！");
		}
 
		Connection con = null;
		try {
			// 获取数据库连接
			con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
			System.out.println("获取数据库连接成功！");
			System.out.println("进行数据库操作！");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("获取数据库连接失败！");
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
 
	}
}

//需要说明的是MySQL8.0版本引入安全加密机制，在地址连接上于以往的版本存在一定的差别

//以往版本的写法：
//static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/student
//8.0版本的写法：
//static final String dbUrl = "jdbc:mysql://123.207.19.222:3306/students?useSSL=false&serverTimezone=CTT";
//其中student是数据库的名称，useSSL是用户是否加密有true和false 两种选择，servertimezone 是服务时区，
//在调试的过程中可能会有这样的错误：
//Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
